<?php
$time = date("Y-m-d H:i:s",time());
if($_POST['type']==null)//get
{
	echo "get<br>";
	echo '<pre>'; print_r($_REQUEST);
	$type = $_REQUEST['type'];
	$log = makelog($_REQUEST);
}
else//post
{
	echo "post<br>";
	echo '<pre>'; print_r($_POST);
	$type = $_POST['type'];
	$log = makelog($_POST);
}

if(file_exists('testlog.txt') == false)
{
	$fopen = fopen($file,'wb');
	fclose($fopen);
}
file_put_contents('testlog.txt', $type.' '.$log ,FILE_APPEND);

$file = 'playlogs/'.$type.'/'.date("Ymd",time()).'.txt';
if (file_exists($file) == false) {
	$fopen = fopen($file,'wb');
	fclose($fopen);
}
if (is_writable($file) == false)
{   
	die('文件无法写入.');
}
else
{   
	file_put_contents($file, $log ,FILE_APPEND);
}

function makelog($arg)
{   
	$line = '';
	$split = ' ';
	$enter = "\r\n";
	$type = $arg['type'];
	$recordTime = '['.date("Y-m-d H:i:s",time()).']';//记录时间
	$guid = 'null';//GUID
	$optime = '['.$arg['timeStart'].']';//[操作时间]
	$success = '1';//成功标识
	$name = '['.$arg['name'].']';//[频道名称/节目名称]
	$uuid = $arg['uuid'];//频道UUID
	$url = $arg['vod'];//点播节目URL
	$vodStartTime = 'null';//点播节目开始时间（MM:SS）
	$userip = get_real_ip();//用户IP
	$requestTimespan = $arg['diffLaunch'];//请求指令完成话费的时长(秒)
	$sbsip = $arg['sbs'];//SBS设备IP
	$scsip = $arg['scs'];//SCS设备IP
	$playTimespan = $arg['diffPlay'];//正常播放画面话费的时长(秒)
	$band = 'null';//视频码流
	$bufferInfo = $arg['diffBuffer'];//缓冲信息项"B HH:MM:SS N BAND HH:MM:SS N BAND"
	$download = 'null';//下载数据量MB
	//foreach ($arg as $key=>$value){$line .= $key.':'.$value[$key].'|';}	 
	if($type == 'live')
	{
		//GUID [操作时间] 成功标识 频道名称 频道UUID 用户IP 请求指令完成话费的时长 SBS设备IP SCS设备IP 正常播放画面话费的时长 视频码流 "B HH:MM:SS N BAND HH:MM:SS N BAND" 操作时间 下载数据量MB
		$line = $recordTime.$split.$guid.$split.$optime.$split.$success.$split.$name.$split.$uuid.$split.$userip.$split.$requestTimespan.$split.$sbsip.$split.$scsip.$split.$playTimespan.$split.$band.$split.$bufferInfo.$split.$optime.$split.$download.$enter;
	}
	else if($type == 'vod')
	{
		//GUID [操作时间] 成功标识 节目名称 URL 用户IP 节目开始时间(MM:SS) 请求指令完成话费的时长 SBS设备IP SCS设备IP 正常播放画面话费的时长 视频码流 "B HH:MM:SS N BAND HH:MM:SS N BAND" 操作时间 下载数据量MB
		$line = $recordTime.$split.$guid.$split.$optime.$split.$success.$split.$name.$split.$url.$split.$userip.$split.$vodStartTime.$split.$requestTimespan.$split.$sbsip.$split.$scsip.$split.$playTimespan.$split.$band.$split.$bufferInfo.$split.$optime.$split.$download.$enter;
	}
	else
	{
		//GUID [操作时间] 成功标识 频道名称 UUID 用户IP SCSIP 请求指令完成花费的时长 视频码流 "B HH:MM:SS N BAND HH:MM:SS N BAND" 操作时间 下载数据量MB
		$line = $recordTime.$split.$guid.$split.$optime.$split.$success.$split.$name.$split.$uuid.$split.$userip.$split.$scsip.$split.$requestTimespan.$split.$band.$split.$bufferInfo.$split.$optime.$split.$download.$enter;
	}
	return $line;
}

function get_real_ip()  
{  
   $ip=false;  
   if(!empty($_SERVER["HTTP_CLIENT_IP"]))  
   {  
		$ip = $_SERVER["HTTP_CLIENT_IP"];  
   }  
   if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
   {  
		$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);  
		if ($ip)  
		{  
			array_unshift($ips, $ip); $ip = FALSE;  
		}  
		for ($i = 0; $i < count($ips); $i++)  
		{  
			if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i]))  
			{  
				   $ip = $ips[$i];  
				   break;  
			}  
		}  
   }  
   return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);  
} 